﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Vibz.SharePoint.Entity;

namespace Vibz.SharePoint
{
    public class SPView
    {
        Views.Views _view;
        public SPView(object siteUrl)
        {
            _view = new Vibz.SharePoint.Views.Views();
            _view.Url = siteUrl.ToString() + "/_vti_bin/Views.asmx";
            _view.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }
        public void AddColumnToDefaultView(string listName, List<ColumnInfo> columns)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode xnDefaultView = _view.GetView(listName, "");
            string sDefaultViewName = xnDefaultView.Attributes["Name"].Value.ToString();
            XmlNode xnViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            foreach(XmlNode childNode in xnDefaultView.ChildNodes)
            {
                if(childNode.Name == "ViewFields")
                {
                    xnViewFields.InnerXml += childNode.InnerXml;
                }
            }
            foreach(ColumnInfo column in columns)
            {
                xnViewFields.InnerXml += "<FieldRef Name = '" + column.ColumnName + "'/>";
            }
            _view.UpdateView(listName, sDefaultViewName, null, null, xnViewFields, null, null, null);
        }
    }
}
